from django.contrib import admin

# Register your models here.
from .models import Video,VideoLabel,Role,VideoSuffix,VideoPath
from pathlib import Path
from django.utils.safestring import mark_safe#导入make_safe

class manageRole(admin.ModelAdmin):
    #显示字段
    list_display = ('role_id','image_data','chinese_name','old_names','area','photo','include_fanhao','include_number')
    #搜索
    search_fields = ('chinese_name','old_names')
    list_filter = ('area',)
    
    #设置每页显示条目数
    list_per_page = 50
    # 创建一个自定义方法
    def image_data(self, obj):
        if obj.photo:
            p = Path(obj.photo)
            name = p.name
            parent_name = p.parent.stem
            #img = Path(parent_name).joinpath(name)
            img = '{}/{}/{}'.format('http://img.com',parent_name,name)
            #使用mark_safe返回内容，否则页面将以html实体展示输出结果
            return mark_safe('<img src="%s" width="80px" height="90px">'%(img))#src紧跟/ 表示根目录

class manageVideo(admin.ModelAdmin):
    list_display = ('role_name','video_label_id','introduction','designation','designation_name','name','suffix_id','mosaic','subtitle')
    #设置每页显示条目数
    list_per_page = 50

    search_fields = ('role_name','name')

    #获取对应的演员名称
    def role_name(self,obj):
        
        role_info = Role.objects.filter(role_id=obj.role_id).first()
        if role_info:
            if role_info.chinese_name:
                return role_info.chinese_name
            elif role_info.old_names:
                return role_info.old_names
            elif role_info.real_name:
                return role_info.real_name

class manageVideoPath(admin.ModelAdmin):
    list_display = ('video_id','role_id','name','path','suffix_id')
    search_fields = ('name',)
    list_per_page = 50
#admin.site.register(Role,list_per_page = 50)
admin.site.register(Role,manageRole)
admin.site.register(Video,manageVideo)
admin.site.register(VideoPath,manageVideoPath)
admin.site.register(VideoLabel,list_per_page = 50)
admin.site.register(VideoSuffix,list_per_page = 50)
